package com.xx.doublexchat.common.user.controller;


import com.xx.doublexchat.common.common.domain.vo.request.CursorPageBaseRequest;
import com.xx.doublexchat.common.common.domain.vo.request.PageBaseRequest;
import com.xx.doublexchat.common.common.domain.vo.response.ApiResult;
import com.xx.doublexchat.common.common.domain.vo.response.CursorPageBaseResponse;
import com.xx.doublexchat.common.common.domain.vo.response.PageBaseResponse;
import com.xx.doublexchat.common.common.utils.RequestHolder;
import com.xx.doublexchat.common.user.domain.entity.FriendApplyResponse;
import com.xx.doublexchat.common.user.domain.vo.request.FriendApplyRequest;
import com.xx.doublexchat.common.user.domain.vo.request.FriendApproveRequest;
import com.xx.doublexchat.common.user.domain.vo.request.FriendCheckRequest;
import com.xx.doublexchat.common.user.domain.vo.request.FriendDeleteRequest;
import com.xx.doublexchat.common.user.domain.vo.response.FriendCheckResponse;
import com.xx.doublexchat.common.user.domain.vo.response.FriendResponse;
import com.xx.doublexchat.common.user.domain.vo.response.FriendUnreadResponse;
import com.xx.doublexchat.common.user.service.IUserFriendService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.validation.Valid;

/**
 * <p>
 * 用户联系人表 前端控制器
 * </p>
 *
 * @author xiaoXuan
 * @since 2024-02-21
 */
@Controller
@RequestMapping("/capi/user/friend")
public class UserFriendController {

    @Autowired
    private IUserFriendService userFriendService;

    @GetMapping("/page")
    @ApiOperation("联系人列表")
    public ApiResult<CursorPageBaseResponse<FriendResponse>> friendList(@Valid CursorPageBaseRequest request) {
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(userFriendService.friendList(uid, request));
    }

    @GetMapping("/check")
    @ApiOperation("批量判断是否为自己的好友")
    public ApiResult<FriendCheckResponse> check(@Valid FriendCheckRequest request) {
        //获取当前用户id
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(userFriendService.check(uid, request));
    }

    @PostMapping("/apply")
    @ApiOperation("申请好友")
    public ApiResult<Void> apply(@Valid @RequestBody FriendApplyRequest request) {
        Long uid = RequestHolder.get().getUid();
        userFriendService.apply(uid, request);
        return ApiResult.success();
    }

    @PutMapping("/apply")
    @ApiOperation("审批同意")
    public ApiResult<Void> applyApprove(@Valid @RequestBody FriendApproveRequest request) {
        userFriendService.applyApprove(RequestHolder.get().getUid(), request);
        return ApiResult.success();
    }

    @GetMapping("/apply/page")
    @ApiOperation("好友申请列表")
    public ApiResult<PageBaseResponse<FriendApplyResponse>> page(@Valid PageBaseRequest request) {
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(userFriendService.pageApplyFriend(uid, request));
    }

    @GetMapping("/apply/unread")
    @ApiOperation("申请未读数")
    public ApiResult<FriendUnreadResponse> unread() {
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(userFriendService.unread(uid));
    }

    @DeleteMapping()
    @ApiOperation("删除好友")
    public ApiResult<Void> delete(@Valid @RequestBody FriendDeleteRequest request) {
        Long uid = RequestHolder.get().getUid();
        userFriendService.deleteFriend(uid, request.getTargetUid());
        return ApiResult.success();
    }
}

